var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();

Page({
  data: {
    addressList: [],
    nowdata:[]
  },
  onLoad: function (options) {
    let that=this;
    that.setData({
      nowdata: options
    });
    // 页面初始化 options为页面跳转所带来的参数
    this.getAddressList();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  getAddressList (){
    let that = this;
    let user_id = wx.getStorageSync('userId');
    let userdata = { user_id: user_id };
    util.request(api.AddressList,userdata).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          addressList: res.data
        });
      }
    });
  },
  addressAddOrUpdate (event) {
    let that = this;
    let nowData = that.data.nowdata;
    if (parseInt(nowData.goodsid) > 0) {
      let url = '/pages/shopping/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId + "&goodsid=" + nowData.goodsid + "&number=" + nowData.number + "&versions_price=" + nowData.versions_price + "&versions_title=" + nowData.versions_title + "&color_price=" + nowData.color_price + "&color_title=" + nowData.color_title + "&color_id=" + nowData.color_id;
      wx.redirectTo({
        url: url
      })
    
    }else{
      wx.navigateTo({
        url: '/pages/shopping/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
      })
    }
  },
  selectAddress(event){
    try {
      wx.setStorageSync('addressId', event.currentTarget.dataset.addressId);
    } catch (e) {

    }
    let that=this;
    let nowData = that.data.nowdata;
    if (parseInt(nowData.goodsid)>0){
      let url = "/pages/shopping/checkout/checkout" +"?goodsid=" + nowData.goodsid + "&number=" + nowData.number + "&versions_price=" + nowData.versions_price + "&versions_title=" + nowData.versions_title + "&color_price=" + nowData.color_price + "&color_title=" + nowData.color_title + "&color_id=" + nowData.color_id;
      wx.redirectTo({
        url:url
      })
    }else{
      //选择该收货地址
      wx.redirectTo({
        url: '/pages/shopping/checkout/checkout'
      })
    }

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})